package com.boot.springBoot.config.mybatis;

import com.alibaba.druid.pool.DruidDataSource;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInterceptor;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Properties;

/**
 * @ClassName: MyBatisCofig
 * @Description: 主数据源配置
 * @author ysp
 * @date 2018年9月21日 上午11:03:38
 * @version V1.0
 */
@Configuration
@MapperScan(basePackages = MyBatisCofig.PACKAGE,sqlSessionFactoryRef = "masterSqlSessionFactory")
@EnableTransactionManagement
public class MyBatisCofig {

	    static final String PACKAGE = "com.boot.springBoot.config.mapper";
	    static final String MAPPER_LOCATION = "classpath:mapper.*.xml";

	/**
	 * 使用 @ConfigurationProperties(prefix = "spring.datasource") 加载所有的配置
	 * 下面的是原来的  太麻烦太繁琐
	 * @return
	 */
	    
	    @Bean(name = "masterDataSource")
	    @ConfigurationProperties(prefix = "spring.datasource")
	    public DataSource masterDataSource() {
	        DruidDataSource dataSource = new DruidDataSource();
	        return dataSource;
	    }

	    @Bean(name = "masterTransactionManager")
//	    @Primary   多个数据源时 指定一个主数据源
	    public DataSourceTransactionManager masterTransactionManager() {
	        return new DataSourceTransactionManager(masterDataSource());
	    }


//	    @Primary
        @Bean(name = "masterSqlSessionFactory")
	    public SqlSessionFactory masterSqlSessionFactory(@Qualifier("masterDataSource") DataSource masterDataSource)
	            throws Exception {
	        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
	        sessionFactory.setDataSource(masterDataSource);
	        sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
	                .getResources(MyBatisCofig.MAPPER_LOCATION));
	        
	        
	        PageHelper pageHelper = new PageHelper();
	        Properties properties = new Properties();
	        properties.setProperty("reasonable", "true");
	        properties.setProperty("supportMethodsArguments", "true");
	        properties.setProperty("returnPageInfo", "check");
	        properties.setProperty("params", "count=countSql");
//	        pageHelper.setProperties(properties);
	        
	        //分页插件
//	        sessionFactory.setPlugins(new Interceptor[]{(Interceptor) pageHelper});
	        PageInterceptor pageInterceptor = new PageInterceptor();
	        pageInterceptor.setProperties(properties);
	        sessionFactory.setPlugins(new Interceptor[]{pageInterceptor});
	        
	        return sessionFactory.getObject();
	    }
	}

